<?php
class vote extends Zcontroller {
	
	function __construct() {
		parent::__construct();
		Zsession::setDataToSession("current_page", "vote");		
	}
	
	function index(){
		$this->load->helper('url_helper');
		$this->load->model('vote_model');
		$data['events']=$this->vote_model->selectEvent();
	    $data['categories']=$this->vote_model->selectCategory();
        $this->load->template("templates/general","voting/vote_list","All Vote",$data);
	}
	function advanced(){
		$this->load->helper('url_helper');
		$this->load->model('vote_model');
		//$data['events']=$this->vote_model->selectEvent();
	    $data['categories']=$this->vote_model->selectCategory();
        $this->load->template("templates/general","voting/advanced_search","Advanced Search",$data);
	}
	
	function getSearch(){
		$cat_id=$_POST['selcat'];
		$keyword=(isset($_POST['txtkeyword']))?$_POST['txtkeyword']:null;
		//echo "url="."vote/search/".$cat_id."/".$keyword;
		redirect(site_url("vote/search/".$cat_id."/".urlencode($keyword)));
	}

	function search($cat_id,$keyword=null){
		$this->load->model('vote_model');
		$data['events']=$this->vote_model->searchEvent($cat_id,urldecode($keyword));
		$data['categories']=$this->vote_model->selectCategory();
		$data['sel_cat_id']=$cat_id;
		$this->load->template("templates/general","voting/vote_list","All Vote",$data);
	}
	
	
	function getAdvancedSearch(){
		$cat_id=$_POST['selcat'];
		$close_from=(isset($_POST['txtclosefrom']))?$_POST['txtclosefrom']:null;
		$close_to=(isset($_POST['txtcloseto']))?$_POST['txtcloseto']:null;
		redirect(site_url("vote/advancedSearch/".$cat_id."/".$close_from."/".$close_to));
		/*if($keyword!=""){
			redirect(site_url("vote/search/".$cat_id."/".$keyword));
		}else{
			redirect(site_url("vote/search/".$cat_id));
		}*/
	}
	function advancedSearch($cat_id,$close_from=null,$close_to=null){
		$this->load->model('vote_model');
		$data['events']=$this->vote_model->selectEventbyClosingDate($cat_id,$close_from,$close_to);
		$data['categories']=$this->vote_model->selectCategory();
		$data['sel_cat_id']=$cat_id;
		$this->load->template("templates/general","voting/vote_list","All Vote",$data);
	}
	
	/*function search($cat_id,$keyword=null){
		$this->load->model('vote_model');
		$data['events']=$this->vote_model->searchEvent($cat_id,$keyword);
		$data['categories']=$this->vote_model->selectCategory();
		$data['sel_cat_id']=$cat_id;
		$this->load->template("templates/general","voting/vote_list","All Vote",$data);
	}*/
	

	function voteDetail($event_id,$sort=null){
		$this->load->model('vote_model');
		$data['events']=$this->vote_model->selectEvent($event_id);
		$data['curevent']=$this->vote_model->selectUserEvent($event_id);
		$curevent=$this->vote_model->selectEvent($event_id);
		
		
		date_default_timezone_set("Asia/Phnom_Penh");
    	$today_date = date("Y-m-d");
    	$data['voting']="0";
    	foreach ($curevent->result_array() as $myevent)	{
    		$data['voting']=($myevent['end_date']>=$today_date)?"1":"0";
    	}
    	
    	
		$events=$data['events']->result();
		$top_id=$events[0]->top_id;
		$myItems=$this->vote_model->selectItem($top_id);
		$data['items']=$this->vote_model->selectItem($top_id,$sort);
		$data['sort']=$sort;
		/* Total Count*/
		$total=0;
		foreach($myItems->result_array() as $myItem){
			$count=$this->vote_model->countVote($event_id,$myItem["vote_item_id"]);
			$total=$total+$count;
		}
		$data['curitems']=$this->vote_model->selectCurrentEvent();
		$data['total']=$total;
        $this->load->template("templates/general","voting/vote_detail","All Vote",$data);
	}
	
	function submitVote(){
		$this->load->model('vote_model');
		$event_id=$_POST['txteventid'];
		$items=(isset($_POST['items']))?$_POST['items']:"";
		$itemCount=0;
		
		//Current Vote
		$curevent=$this->vote_model->selectEvent($event_id);
		date_default_timezone_set("Asia/Phnom_Penh");
    	$today_date = date("Y-m-d");
    	$data['voting']="0";
    	foreach ($curevent->result_array() as $myevent)	{
    		$data['voting']=($myevent['end_date']>=$today_date)?"1":"0";
    	}
    	
		
		//Get Maximum Number of Items to be Chosen
		$events=$this->vote_model->selectEvent($event_id);
		$thisEvent=$events->result();
		$itemMax=$thisEvent[0]->top_item_max;
		$itemCount=count($items);
		
		$data['events']=$this->vote_model->selectEvent($event_id);
		$data['curevent']=$this->vote_model->selectUserEvent($event_id);
		$events=$data['events']->result();
		$top_id=$events[0]->top_id;
		
		$allItems=$this->vote_model->selectItem($top_id);
		$arrCounts=array();
		$i=0;
		
		//Get vote count
		/*foreach($allItems->result_array() as $citem){
			$citem[$i]=$this->vote_model->countVote($event_id,$citem["vote_item_id"]);
			$i++;
		}
		//arsort($citem);
		for($y=0;$y<count($citem);$y++){
			echo $citem[$y]."<br/>";
		}*/
		
		$data['items']=$this->vote_model->selectItem($top_id);
		if(!empty($items)){
			if($itemCount>$itemMax){
				$data["max_error"]="Please choose at most ".$itemMax;
				$data["checkeditems"]=$items;
        		$this->load->template("templates/general","voting/vote_detail","All Vote",$data);
			}else{
			$this->vote_model->deleteVoteEvent($event_id);
			for($i=0;$i<$itemCount;$i++){
				$this->vote_model->insertVote($event_id,$items[$i]);
			}
			redirect(site_url("vote"));
			}
		}else{
			$this->vote_model->deleteVoteEvent($event_id);
			redirect(site_url("vote"));
		}
		
	}
}

